package com.gxuwz.ssy.common;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 这里自定义了一个AuthenticationProvider来处理实际的认证业务逻辑，在这里可以方便的根据我们需要来进行自定义，
 * 做验证码校验、效期校验和验密，可以根据需要定制。认证成功就返回一个UsernamePasswordAuthenticationToken对象并配置好合适的权限
 * 如果认证失败，只需要抛出一个异常（AuthenticationException的子类），
 * @author LiaoBaohong 2021/7/14
 */
@Component
@Slf4j
public class MyAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private CustomUserDetailsService myUserDetailService;


    @SneakyThrows
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 获取用户的输入
        MyWebAuthenticationDetails details = (MyWebAuthenticationDetails) authentication.getDetails();

        String username = details.getUsername();


        String dbPassword = myUserDetailService.loadUserByUsername(username).getPassword();
        System.out.println("dbPassword = " + dbPassword);
        String password  = details.getPassword() ;
        System.out.println("用户输入password = " + password);
        if (!dbPassword.equals(password)){
            log.info("密码错误");
            throw new BadCredentialsException("密码错误");
        }

        // TODO 赋予权限,后期改成数据库权限
        Collection<GrantedAuthority> auths =
                (Collection<GrantedAuthority>) myUserDetailService.loadUserByUsername(username).getAuthorities();
        return new UsernamePasswordAuthenticationToken(details.getUsername(), details.getPassword(), auths);

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}